package org.gruposp2p.dnie.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.gruposp2p.dnie.model.xmladapter.DateAdapter;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "DNIeUser")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "DNIEUSER", catalog = "DNIE")
@NamedQueries({@NamedQuery(name = "DNIeUser.findAll", query = "SELECT d FROM DNIeUser d"), @NamedQuery(name = "DNIeUser.findById", query = "SELECT d FROM DNIeUser d WHERE d.id = :id"), @NamedQuery(name = "DNIeUser.findByNif", query = "SELECT d FROM DNIeUser d WHERE d.nif = :nif"), @NamedQuery(name = "DNIeUser.findByName", query = "SELECT d FROM DNIeUser d WHERE d.name = :name"), @NamedQuery(name = "DNIeUser.findByPassword", query = "SELECT d FROM DNIeUser d WHERE d.password = :password"), @NamedQuery(name = "DNIeUser.findByEmail", query = "SELECT d FROM DNIeUser d WHERE d.email = :email"), @NamedQuery(name = "DNIeUser.findByCertdata", query = "SELECT d FROM DNIeUser d WHERE d.certdata = :certdata"), @NamedQuery(name = "DNIeUser.findByPhone", query = "SELECT d FROM DNIeUser d WHERE d.phone = :phone"), @NamedQuery(name = "DNIeUser.findByDateAdded", query = "SELECT d FROM DNIeUser d WHERE d.dateAdded = :dateAdded")})
public class DNIeUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @XmlElement
    private Integer id;
    @Column(name = "NIF", length = 24)
    @XmlElement
    private String nif;
    @Column(name = "NAME", length = 255)
    @XmlElement
    private String name;
    @Column(name = "PASSWORD", length = 255)
    private String password;
    @Column(name = "EMAIL", length = 50)
    @XmlElement
    private String email;
    @Column(name = "CERTDATA", length = 500)
    private String certdata;
    @Column(name = "PHONE", length = 24)
    @XmlElement
    private String phone;
    @Column(name = "DATEADDED")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date dateAdded;
    @Basic(optional = false)
    @Column(name = "ENABLED", nullable = false)
    private Boolean enabled;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieUserId")
    private Collection<DNIeSignedDocument> dnieSignedDocumentCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieUserId")
    private Collection<DNIeDocumentToSign> dnieDocumentToSignCollection;
    //@JoinColumn(name = "DNIEUSERTYPE_ID", referencedColumnName = "ID")
    //@ManyToOne
    //private DNIeUserType dnieUserTypeId;
    @Column(name = "AUTHORITY", length = 50)
    @Enumerated(value = EnumType.STRING)
    private DNIeAuthority authority;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieUserId")
    private Collection<DNIeSignedDocumentComment> dnieSignedDocumentCommentCollection;

    public DNIeUser() {
    }

    public DNIeUser(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNif() {
        return nif;
    }

    public void setNif(String nif) {
        this.nif = nif;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCertdata() {
        return certdata;
    }

    public void setCertdata(String certdata) {
        this.certdata = certdata;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Collection<DNIeSignedDocument> getDNIeSigneddocumentCollection() {
        return dnieSignedDocumentCollection;
    }

    public void setDNIeSigneddocumentCollection(Collection<DNIeSignedDocument> dnieSignedDocumentCollection) {
        this.dnieSignedDocumentCollection = dnieSignedDocumentCollection;
    }

    public Collection<DNIeDocumentToSign> getDNIeDocumentTosignCollection() {
        return dnieDocumentToSignCollection;
    }

    public void setDNIeDocumentTosignCollection(
            Collection<DNIeDocumentToSign> dnieDocumentToSignCollection) {
        this.dnieDocumentToSignCollection = dnieDocumentToSignCollection;
    }

    public DNIeAuthority getAuthority() {
        return authority;
    }

    public void setAuthority(DNIeAuthority authority) {
        this.authority = authority;
    }

    public Collection<DNIeSignedDocumentComment> getDNIeSignedDocumentCommentCollection() {
        return dnieSignedDocumentCommentCollection;
    }

    public void setDNIeSignedDocumentCommentCollection(
            Collection<DNIeSignedDocumentComment> dnieSignedDocumentCommentCollection) {
        this.dnieSignedDocumentCommentCollection = dnieSignedDocumentCommentCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof DNIeUser)) {
            return false;
        }
        DNIeUser other = (DNIeUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "dniedatabasemodel01.DNIeUser[id=" + id + "]";
    }

    public GrantedAuthority[] getAuthorities() {
        return new GrantedAuthority[] { new GrantedAuthorityImpl(
                        authority.toString()) };
    }

    public String getUsername() {
        return name;
    }

    public boolean isAccountNonExpired() {
        return enabled;
    }

    public boolean isAccountNonLocked() {
        return enabled;
    }

    public boolean isCredentialsNonExpired() {
        return enabled;
    }

    public boolean isEnabled() {
        return enabled;
    }

}
